package com.zuo.seckilldemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zuo.seckilldemo.entity.User;
import com.zuo.seckilldemo.handler.GlobalException;
import com.zuo.seckilldemo.mapper.UserMapper;
import com.zuo.seckilldemo.result.CodeMsg;
import com.zuo.seckilldemo.result.Result;
import com.zuo.seckilldemo.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zuo.seckilldemo.utils.CookieUtils;
import com.zuo.seckilldemo.utils.MD5Util;
import com.zuo.seckilldemo.utils.UUIDUtil;
import com.zuo.seckilldemo.vo.LoginVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zuogaocan
 * @since 2022-11-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    
    @Override
    public String login(HttpServletResponse response, LoginVo loginVo) {
        if (loginVo ==null){
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
        String mobile_number = loginVo.getMobile_number();
        String password = loginVo.getPassword();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("mobile_number",mobile_number);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null){ 
            throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }
        String dbPass = user.getPassword();
        String dbSalt = user.getSalt();
        String pass = MD5Util.frontEndPassToDB(password, dbSalt);

        if (!pass.equals(dbPass)){
            throw new GlobalException(CodeMsg.PASSWORD_ERROR);
        }
        String token = UUIDUtil.uuid();
        //将用户信息存入redis中
        addCookie(response,token,user);
        return token;
    }
    public void addCookie(HttpServletResponse response, String token, User user) {
        redisTemplate.opsForValue().set("user:"+ token,user,3600, TimeUnit.SECONDS);
        Cookie cookie = new Cookie("userToken", token);
        cookie.setMaxAge(360000);
        cookie.setPath("/");//设置为网站根目录
        response.addCookie(cookie);
    }
    @Override
    public User getByToken(String token, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(token)) return null;
        User user = (User) redisTemplate.opsForValue().get("user:" + token);
        if (user!=null){
            CookieUtils.setCookie(request,response,"userToken",token,360000,true);
        }
        return user;
    }
    
}
